// pages/familyFeast/index.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "家宴", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    dateList: [],
    bmNum: 0,
    chooseDate: -1, //日期选择
    sjdArr: [{
        id: 0,
        img: 'images/familyFeast/lunch.png',
        isBlack: 0
      },
      {
        id: 1,
        img: 'images/familyFeast/dinner.png',
        isBlack: 0
      }
    ],
    chooseTime: -1,
    showMonment: true, //是否显示须知

    openid: '',
    shareTitle: '',
    shareImg: '',
    lunchSyNum: 0, //午餐剩余数量
    dinnerSyNum: 0 //晚餐剩余数量
  },

  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  add() {
    let that = this;
    let bmNum = that.data.bmNum;

    bmNum++;
    that.setData({
      bmNum
    })
  },
  reduce() {
    let that = this;
    let bmNum = that.data.bmNum;
    if (bmNum > 0) {
      bmNum--;
      that.setData({
        bmNum
      })
    }

  },
  rule() {
    let that = this;
    that.setData({
      showMonment: true
    })
  },
  //点击我知晓了
  closeMonment() {
    let that = this;
    that.setData({
      showMonment: false
    })
  },
  //跳转我的订单页
  myOrder() {
    wx.navigateTo({
      url: '/pages/familyFeast/orderList',
    })
  },
  //选择日期
  chooseDate(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;

    var list = that.data.dateList;
    if (list[id]['status'] == '暂停预约') {
      app.showTip('抱歉,您选择的时间已经暂停预约了哦,请选择其他时间预约');
      return;
    } else if (list[id]['status'] == '预约已满') {
      app.showTip('抱歉,您选择的时间预约人数已满了哦,请选择其他时间预约');
      return;
    }


    wx.showLoading({
      title: '时间段状态切换中...'
    })
    //后台请求时段是否满了
    wx.request({
      url: that.data.baseUrl + 'checkSjcIsBmLy_jy.php',
      method: 'POST',
      data: {
        bm_time: list[id].ymd
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.status == 1) {
          //更新剩余数量
          that.setData({
            lunchSyNum: res.data.lunchSyNum,
            dinnerSyNum: res.data.dinnerSyNum
          });
          if (res.data.sjdArr[0].is_black == 1) {
            //午餐预约已满了
            that.setData({
              ['sjdArr[0].isBlack']: 1
            })
            if (that.data.chooseTime == 0) {
              that.setData({
                chooseTime: -1
              })
            }
          } else {
            that.setData({
              ['sjdArr[0].isBlack']: 0,
              chooseTime: -1
            })
          }

          if (res.data.sjdArr[1].is_black == 1) {
            //晚餐预约已满了
            that.setData({
              ['sjdArr[1].isBlack']: 1
            })

            if (that.data.chooseTime == 1) {
              that.setData({
                chooseTime: -1
              })
            }
          } else {
            that.setData({
              ['sjdArr[1].isBlack']: 0,
              chooseTime: -1
            })
          }

          //更改选择的时间状态
          that.setData({
            chooseDate: id
          });
        } else {
          app.showTip(res.data.msg);
        }
        wx.hideLoading();
      },
      fail: function () {
        wx.hideLoading();
      }
    });
  },

  //点击预约时间按钮
  order() {
    var that = this;
    //逻辑判断
    if (that.data.chooseDate == -1) {
      app.showTip('请选选择日期');
      return;
    } else if (that.data.chooseTime == -1) {
      app.showTip('请选选择时段');
      return;
    } else if (that.data.bmNum == 0) {
      app.showTip('请填写用餐人数');
      return;
    } else {
      //跳转商品列表页
      wx.navigateTo({
        url: '/pages/familyFeast/menu?people_number=' + that.data.bmNum + '&bm_time=' + that.data.dateList[that.data.chooseDate].ymd + "&yy_time=" + (that.data.chooseTime + 1)
      })
    }
  },

  //时段选择
  chooseTime(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    //判断是否预约已满了
    if (that.data.sjdArr[id].isBlack == 1) {
      return;
    }
    that.setData({
      chooseTime: id
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    //判断是否弹出过了规则弹窗
    /*if (wx.getStorageSync('jy_rule') == '') {
      //弹出规则弹窗
      that.setData({
        showMonment: true
      })
      //重置缓存
      wx.setStorageSync('jy_rule', 1);
    }*/

    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');

    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getFamilyFeastIndexPageInfo(res.data.openid);
              },
              fail: function (res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getFamilyFeastIndexPageInfo(openid);
    }

    //调用分享
    that.getShareInfo();
  },

  //获取本页数据
  getFamilyFeastIndexPageInfo(openid) {
    var that = this;

    wx.showLoading({
      title: '数据加载中...'
    });

    wx.request({
      url: that.data.baseUrl + 'getFamilyFeastIndexPageInfo.php',
      method: 'POST',
      data: {
        openid: openid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //后台请求成功
          that.setData({
            dateList: res.data.list
          });
        } else {
          //后台请求失败
          app.showTip(res.data.msg);
          setTimeout(function () {
            wx.redirectTo({
              url: '/pages/lifeService/index'
            });
          }, 2000)
          return;
        }
      },
      fail: function (res) {
        wx.hideLoading();
        app.showTip('服务器繁忙,请稍候重试');
        setTimeout(function () {
          wx.redirectTo({
            url: '/pages/lifeService/index'
          });
        }, 1500)
        return;
      }
    });
  },

  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo_order.php',
      method: 'POST',
      data: {
        pid:13
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function (res) {}
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/familyFeast/index',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  }
})